//#
//# Task switch code
//#
#include <csp_macros.h>

.data
	.align 4

.text
#----------------------------------------------------------------------------
# TASK YIELD
#----------------------------------------------------------------------------
	.align 4
	.global __yield
	.type __yield, @function
__yield:
	ai $sp, $sp, -(NCALLEESAVES*8)
	ai $sp, $sp, -(NCALLEESAVES*8)

	lqr $10, current
	stqd $lr, 0($10)
	stqd $sp, 16($10)
	stqd $2, 32($10)

	#stqa $lr, current 
	#stqa $sp, current + 16
	#stqa $2,  current + 32 # environment pointer
	REGPT = 80
.rept NCALLEESAVES
	stqd $REGPT, -16*(REGPT-80)($sp)
	REGPT = REGPT +1
.endr
	/* restore kernel sp */
	lqa $sp, kernel_stackpointer

	/* jump to main_loop */
	br main_loop


#----------------------------------------------------------------------------
# TASK RESUME
#----------------------------------------------------------------------------
	.align 4
	.global __resume
	.type __resume, @function
__resume:
	# load address
	lqr $10, current
	lqd $lr, 0($10)
	lqd $sp, 16($10)
	lqd $2, 32($10)

	#lqa $lr, current 
	#lqa $sp, current + 16 
	#lqa $2,  current + 32 

	REGPT = 80
.rept NCALLEESAVES
	lqd $REGPT, -16*(REGPT-80)($sp)
	REGPT = REGPT +1
.endr
	ai $sp, $sp, NCALLEESAVES*8
	ai $sp, $sp, NCALLEESAVES*8
	bi  $lr
